<template>
  <el-dialog
    title="提示"
    :visible="setRoleDialog"
    width="30%"
    @close="$emit('update:set-role-dialog',false)"
  >
    <!-- 分配角色 -->
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roles" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('update:set-role-dialog',false)">取 消</el-button>
      <el-button type="primary" @click="onSave">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/company'
import { getInfoById } from '@/api/user'
import { updateEmployeesRoles } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    setRoleDialog: {
      type: Boolean,
      required: true
    },
    currentEmployeesId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      checkList: [],
      roles: []
    }
  },

  created() {
    this.loadRolesList()
    this.getUserRole()
  },

  methods: {
    // 加载所有角色
    async loadRolesList() {
      const res = await getRoleList()
      const res2 = await getRoleList({ page: 1, pagesize: res.total })
      this.roles = res2.rows
    },

    // 获取当前用户信息
    async getUserRole() {
      const res = await getInfoById(this.currentEmployeesId)
      this.checkList = res.roleIds
    },
    async onSave() {
      if (!this.checkList.length) {
        return this.$message.error('权限不能为空')
      }
      await updateEmployeesRoles({ id: this.currentEmployeesId, roleIds: this.checkList })
      this.$message.success('分配权限成功')
      this.$emit('update:set-role-dialog', false)
    }
  }
}
</script>

<style scoped>

</style>
